

//TODO: 注册用户，除了total其他都报组织机构不存在 修改用户 菜单权限 highchart 不出来
var server = require('api');
var _alert = require('alert');
var _clear = require('clearForm');

var userinfo = avalon.define({
	$id : 'userinfo',
	userType: [''],
    info: '',

    dep: [], //总队科室列表
    branch: [], //队伍列表
    // branchdep: [], //支队科室列表
    showTotalDep: false,
	showBranch: false,
	showBranchDep: false,

	userData : [],
	add: {
		name: '',
        userAccount: '',
        userPassword: '',
        userType: '',
        ownId: ''
	},
	query: {
		name: '',
		type: ''
	},
	modify: {
		userId: '',
		userAccount : '',
        userName: '',
		newPassword: '',
		token: ''
	},//user modify data

	init: function () {
		// body...
		avalon.log("usertable init"+_.now());

		setTimeout(function(argument) {
			// body...
				$(function () {
                    queryuser();
			})
		},100)
	},

    //modal
    modifyModal: function () {
        _clear($('#modifyModal'));

	    if (userinfo.info){
	        $('#modifyModal').modal('show');
	        var data = userinfo.info;
	        userinfo.modify.userName = data.userName;
	        userinfo.modify.userAccount = data.userAccount;

        }else {
	        _alert('请先选择用户')
        }

    },

    delModal: function () {

        if (userinfo.info){
            $('#delModal').modal('show');
        }else {
            _alert('请先选择用户')
        }

    },

    // 删除用户

    delUser:function () {

        $.ajax({
            url: server.delUser + 'userId=' + avalon.vmodels.root.user.id + '&userToken=' + avalon.vmodels.root.user.token
            + '&deleteUserId=' + userinfo.info.userId,
            type: 'delete',
            dataType: 'json',
        })
            .fail(function(res) {
                $('#userTable').bootstrapTable({});
                _alert('网络错误，请重试');
            })
            .done(function(res) {
                var data = res.values;
                $('#delModal').modal('hide');
                if(res.status == '9200'){
                    _alert('删除成功');
                    queryuser();
                }else{
                    $('#delModal').modal('hide');
                    queryuser();
                    _alert(res.description);
                }
                // userTable.userData = data.values;

            });

    },

    // 添加用户
    addyUser: function () {

		var data = {
            userId: avalon.vmodels.root.user.id,
            userToken:avalon.vmodels.root.user.token,
            ownId: userinfo.add.ownId,
            userName:userinfo.add.name,
            userAccount:userinfo.add.userAccount,
            userPassword:userinfo.add.userPassword,
            userType:userinfo.add.userType
        };

        $.ajax({
            url: server.adduser,
            type: 'post',
            dataType: 'json',
            headers: {
                'Content-Type': 'application/json'
            },
            data: JSON.stringify(data)
        })

            .done(function(res) {
                var data = res.values;
                if(res.status == '9200'){
                	_alert('新增用户成功');
                    $('#addModal').modal('hide');
                    queryuser();
                }else{
                    _alert(res.description);
                }
            })
        .fail(function(res) {
            avalon.log(JSON.parse(res) + _.now())
        })
    },

    // 查询用户详细信息
    queryUserInfo: function () {
        $.ajax({
            url: server.queryUser,
            type: 'get',
            dataType: 'json',
            data: {
                userId: avalon.vmodels.root.user.id,
                userToken:avalon.vmodels.root.user.token,
                userAccount:userinfo.query.name,
                userType:userinfo.query.type
            }
        })
            .fail(function(res) {
                $('#userTable').bootstrapTable({});
                avalon.log(JSON.parse(res) + _.now())
            })
            .done(function(res) {
                var data = res.values;
                if(res.status == '9200'){
                    $('#userTable').bootstrapTable('load', data);
                    $('#userTable').bootstrapTable('selectPage', data);
                }else{
                    $('#userTable').bootstrapTable('removeAll');
                    $('#userTable').bootstrapTable({});
                }
                // userTable.userData = data.values;

            });
    },
	modifyUser : function(){
        userinfo.modify.userId = userinfo.info.userId;

		var data = {
            userId: avalon.vmodels.root.user.id,
            userToken:avalon.vmodels.root.user.token,
            updateUserId:userinfo.modify.userId,
            userName:userinfo.modify.userName,
            userAccount:userinfo.modify.userAccount,
            userPassword:userinfo.modify.newPassword
        }

		$.ajax({
			method: "put",
			url: server.upUser,
			dataType: 'json',
			headers: { 
		        'Content-Type': 'application/json' 
		    },
			//这⾥是取vm的数据模型 ,通过JSON.stringify会去掉其所有⽅法, 变成JSON字符串
			//再⽤JSON.parse变回纯JS对象
			data: JSON.stringify(data)
		})
		.done(function(res) {
			// body...

            if(res.status == '9200'){
                $('#modifyModal').modal('hide');
                queryuser();
            }else{
                _alert(res.description)
            }

		})
		.fail(function(errormessage) {
		    _alert('网络错误，请重试');
		});
	}
})

//获取用户列表
function queryuser() {

    userinfo.info = '';

    //判断是否为total用户
    if(avalon.vmodels.root.user.userType == 'total'){
        userinfo.userType = avalon.mix(true, [], [{type: 'total',name: '总队'},{type:'totaldep',name: '总队科室'},{type:'branch',name:'支队'}]);
    }else{

        setTimeout(function () {
            $('#userTypeInput').css('display','none');
        },1)

        userinfo.userType = avalon.mix(true, [], [{type:'branchdep',name:'支队科室'}]);
    }

    $.ajax({
        url: server.queryUser,
        type: 'get',
        dataType: 'json',
        data: {
            userId: avalon.vmodels.root.user.id,
            userToken:avalon.vmodels.root.user.token,
        }
    })
        .done(function(res) {
            var data = res;
            if (res.status == '9200'){
                // userTable.userData = data.values;
                $('#userTable').bootstrapTable({});

                setTimeout(function () {
                    $('#userTable').bootstrapTable('load', data.values);

                    $('#userTable').bootstrapTable('selectPage', 1);

                    $('#userTable').on('check.bs.table', function (e, row,ele) {
                        userinfo.info = row;
                    })

                    //hide
                    $('#addModal').on('show.bs.modal', function (e) {
                        _clear($('#addModal'));
                    })


                },100)
            }else{
                $('#userTable').bootstrapTable('removeAll');
                $('#userTable').bootstrapTable({});
            }

        })
        .fail(function(res) {
            $('#userTable').bootstrapTable({});
        })

}

//获取科室列表
function queryDep() {

    $.ajax({
        url: server.officeInfo,
        type: 'get',
        dataType: 'json',
        data: {userId: avalon.vmodels.root.user.id,userToken:avalon.vmodels.root.user.token}
    })
        .done(function (res) {
            if(res.status == '9200'){
                var data = res.values;
                userinfo.dep = avalon.mix(true, [], data);
            }else {
                _alert(res.description)
            }
        })
        .fail(function (res) {
        })
}

//获取队伍列表
function queryTeam() {

    $.ajax({
        url: server.queryTeam,
        type: 'get',
        dataType: 'json',
        data: {userId: avalon.vmodels.root.user.id,userToken:avalon.vmodels.root.user.token,groupType:'branch_group'}
    })
        .done(function(res) {
            $('#lodaingModal').modal('hide')
            var data = res.values;
            if(res.status == '9200'){
                userinfo.branch = avalon.mix(true, [], data);
            }else{
                _alert(res.description);
            }
        })
        .fail(function() {
        })
}

//监控注册用户时选择的用户类型
userinfo.$watch("add.userType",function () {
    var type = userinfo.add.userType;
    switch (type){

        // 为总队科室时，显示总队科室列表，选科室
        case 'totaldep' :
            userinfo.showTotalDep = true;
            userinfo.showBranch = false;
            userinfo.showBranchDep = false;
            queryDep();
            break;

        // 为支队时，显示支队队伍,选队伍
        case 'branch' :
            userinfo.showTotalDep = false;
            userinfo.showBranch = true;
            userinfo.showBranchDep = false;
            queryTeam();
            break;

        // 支队科室时，选择支队的科室
        case 'branchdep' :
            userinfo.showTotalDep = false;
            userinfo.showBranch = false;
            userinfo.showBranchDep = true;
            queryDep();
            break;

        default :
            userinfo.showTotalDep = false;
            userinfo.showBranch = false;
            userinfo.showBranchDep = false;

            userinfo.add.ownId = avalon.vmodels.root.user.groupId;
    }
})

module.exports = userinfo;